<template>
  <header class="navbar">
    <div class="container">
      <!-- Logo -->
      <div class="logo" @click="goToHome">
        <el-icon class="icon-large"><VideoPlay /></el-icon>
        <span class="logo-text">哔哩哔哩</span>
      </div>

      <!-- 导航菜单 -->
      <nav class="nav-menu">
        <router-link to="/" class="nav-item" active-class="active">首页</router-link>
        <router-link v-for="type in videoTypes" :key="type.type_id" :to="'/category/' + type.type_id" class="nav-item" active-class="active">
          {{ type.type_name }}
        </router-link>
      </nav>

      <!-- 用户操作 -->
      <div class="user-actions" v-if="isLoggedIn">
        <el-dropdown>
          <span class="user-info">
            <el-avatar :size="32" :src="userAvatar">{{ currentUser?.username?.[0] }}</el-avatar>
            <span class="username">{{ currentUser?.username }}</span>
            <el-icon class="el-icon--right"><ArrowDown /></el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item @click="goToUserProfile">个人中心</el-dropdown-item>
              <el-dropdown-item @click="goToUserCollections">我的收藏</el-dropdown-item>
              <el-dropdown-item @click="goToUserLikes">我的点赞</el-dropdown-item>
              <el-dropdown-item @click="goToUserComments">我的评论</el-dropdown-item>
              <el-dropdown-divider />
              <el-dropdown-item @click="handleLogout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
      <div class="user-actions" v-else>
        <el-button type="text" @click="goToLogin">登录</el-button>
        <el-button type="primary" @click="goToRegister">注册</el-button>
      </div>
    </div>
    
    <!-- 搜索框区域 - 移动到导航栏下方 -->
    <div class="search-container">
      <div class="container">
        <div class="search-box-large">
          <el-input
            v-model="searchKeyword"
            placeholder="搜索视频"
            prefix-icon="Search"
            suffix-icon="Delete"
            @keyup.enter="handleSearch"
            @click:suffix="clearSearch"
          />
        </div>
      </div>
    </div>
  </header>
</template>

<script>
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../store/userStore'
import { useVideoStore } from '../store/videoStore'
import { VideoPlay, ArrowDown, Search, Delete } from '@element-plus/icons-vue'
export default {
  name: 'NavBar',
  components: {
    VideoPlay,
    ArrowDown,
    Search,
    Delete
  },
  setup() {
    const router = useRouter()
    const userStore = useUserStore()
    const videoStore = useVideoStore()
    const searchKeyword = ref('')

    onMounted(() => {
      userStore.loadUserFromStorage()
      videoStore.fetchVideoTypes()
    })

    const isLoggedIn = computed(() => userStore.isLoggedIn)
    const currentUser = computed(() => userStore.currentUser)
    const videoTypes = computed(() => videoStore.videoTypes)
    const userAvatar = computed(() => {
      // 简化处理，实际应该从用户数据中获取
      const avatarColors = ['#f56c6c', '#e6a23c', '#5cb87a', '#1989fa', '#6f7ad3', '#909399']
      const index = currentUser.value?.user_id ? (currentUser.value.user_id - 1) % avatarColors.length : 0
      return avatarColors[index]
    })

    const handleSearch = () => {
      if (searchKeyword.value.trim()) {
        router.push({ path: '/search', query: { keyword: searchKeyword.value } })
      }
    }

    const clearSearch = () => {
      searchKeyword.value = ''
    }

    const goToHome = () => {
      router.push('/')
    }

    const goToLogin = () => {
      router.push('/login')
    }

    const goToRegister = () => {
      router.push('/register')
    }

    const goToUserProfile = () => {
      router.push('/profile')
    }

    const goToUserCollections = () => {
      router.push({ path: '/profile', query: { tab: 'collections' } })
    }

    const goToUserLikes = () => {
      router.push({ path: '/profile', query: { tab: 'likes' } })
    }

    const goToUserComments = () => {
      router.push({ path: '/profile', query: { tab: 'comments' } })
    }

    const handleLogout = () => {
      userStore.logout()
      router.push('/')
    }

    return {
      searchKeyword,
      isLoggedIn,
      currentUser,
      videoTypes,
      userAvatar,
      handleSearch,
      clearSearch,
      goToHome,
      goToLogin,
      goToRegister,
      goToUserProfile,
      goToUserCollections,
      goToUserLikes,
      goToUserComments,
      handleLogout
    }
  }
}
</script>

<style scoped>
/* 全局样式重置和基础设置 */
:deep(.el-button--primary) {
  background-color: #00b42a;
  border-color: #00b42a;
}

:deep(.el-button--primary:hover) {
  background-color: #00a628;
  border-color: #00a628;
}

:deep(.el-input__wrapper) {
  border-radius: 20px;
  box-shadow: none;
  transition: all 0.3s ease;
  background-color: rgba(255, 255, 255, 0.1);
}

:deep(.el-input__wrapper:hover) {
  box-shadow: 0 2px 12px 0 rgba(0, 180, 42, 0.2);
  background-color: rgba(255, 255, 255, 0.15);
}

:deep(.el-input__wrapper:focus-within) {
  box-shadow: 0 0 0 2px rgba(0, 180, 42, 0.3);
  border-color: #00b42a;
  background-color: rgba(255, 255, 255, 0.15);
}

:deep(.el-input__inner) {
  color: #ffffff;
  background-color: transparent;
}

:deep(.el-input__placeholder) {
  color: rgba(255, 255, 255, 0.6);
}

/* 导航栏主体 - 深色主题 */
.navbar {
  background: linear-gradient(135deg, #181818, #1f1f1f);
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.3);
  position: sticky;
  top: 0;
  z-index: 1000;
  transition: all 0.3s ease;
}

/* 滚动时的导航栏效果 */
.navbar.scrolled {
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.4);
  background: linear-gradient(135deg, #121212, #181818);
}

/* 容器样式 */
.container {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 64px;
}

/* Logo样式 */
.logo {
  display: flex;
  align-items: center;
  cursor: pointer;
  font-size: 28px;
  font-weight: bold;
  margin-right: 40px;
  transition: transform 0.3s ease;
}

.logo:hover {
  transform: scale(1.05);
}

.icon-large {
  font-size: 32px;
  margin-right: 8px;
  color: #ffffff;
  filter: drop-shadow(0 2px 4px rgba(0, 180, 42, 0.4));
}

.logo-text {
  font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif;
  background: linear-gradient(45deg, #ffffff, #e0e0e0);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

/* 导航菜单样式 */
.nav-menu {
  display: flex;
  gap: 24px;
  flex: 1;
  margin: 0 auto;
}

.nav-item {
  color: rgba(255, 255, 255, 0.8);
  font-size: 16px;
  font-weight: 500;
  position: relative;
  padding: 10px 15px;
  border-radius: 8px;
  transition: all 0.3s ease;
  text-decoration: none;
  overflow: hidden;
}

.nav-item::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 50%;
  width: 0;
  height: 3px;
  background: #00b42a;
  transform: translateX(-50%);
  transition: width 0.3s ease;
}

.nav-item:hover {
  color: #ffffff;
  background: rgba(255, 255, 255, 0.1);
}

.nav-item:hover::after {
  width: 60%;
}

.nav-item.active {
  color: #00b42a;
}

.nav-item.active::after {
  width: 60%;
}

/* 搜索框区域 - 深色主题 */
.search-container {
  background: linear-gradient(135deg, #1f1f1f, #262626);
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  padding: 16px 0;
  transition: all 0.3s ease;
}

.search-box-large {
  width: 100%;
  max-width: 650px;
  margin: 0 auto;
}

/* 用户操作区域 */
.user-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 20px;
  transition: all 0.3s ease;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.user-info:hover {
  background: rgba(255, 255, 255, 0.1);
  box-shadow: 0 2px 8px rgba(0, 180, 42, 0.2);
  transform: translateY(-2px);
  border-color: rgba(0, 180, 42, 0.3);
}

:deep(.el-avatar) {
  border: 2px solid rgba(255, 255, 255, 0.2);
  box-shadow: 0 2px 4px rgba(0, 180, 42, 0.3);
  transition: all 0.3s ease;
}

.user-info:hover :deep(.el-avatar) {
  transform: scale(1.1);
  border-color: #00b42a;
}

.username {
  font-size: 14px;
  font-weight: 500;
  color: rgba(255, 255, 255, 0.8);
  transition: color 0.3s ease;
}

.user-info:hover .username {
  color: #ffffff;
}

/* 按钮样式优化 */
:deep(.el-button) {
  border-radius: 20px;
  transition: all 0.3s ease;
  font-weight: 500;
}

:deep(.el-button--text) {
  color: rgba(255, 255, 255, 0.7);
}

:deep(.el-button--text:hover) {
  color: #ffffff;
  background: rgba(255, 255, 255, 0.1);
}

/* 下拉菜单容器样式 - 极高优先级覆盖 */
.navbar-container :deep(.el-dropdown-menu) {
  --el-dropdown-menu-bg-color: #2a2a2a !important;
  --el-dropdown-menu-border-color: rgba(255, 255, 255, 0.3) !important;
  border-radius: 12px !important;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3) !important;
  border: 1px solid rgba(255, 255, 255, 0.3) !important;
  overflow: hidden !important;
  background-color: #2a2a2a !important;
}

/* 极高优先级覆盖所有菜单项状态 */
.navbar-container :deep(.el-dropdown-menu__item) {
  --el-dropdown-menu-item-hover-color: #ffffff !important;
  --el-dropdown-menu-item-active-color: #ffffff !important;
  color: #ffffff !important;
  font-weight: 700 !important;
  font-size: 14px !important;
  background-color: transparent !important;
  padding: 12px 20px !important;
  display: block !important;
  text-decoration: none !important;
}

/* 确保所有交互状态下文字可见 */
.navbar-container :deep(.el-dropdown-menu__item:hover) {
  color: #ffffff !important;
  background-color: rgba(0, 180, 42, 0.15) !important;
}

.navbar-container :deep(.el-dropdown-menu__item.is-active) {
  color: #ffffff !important;
  background-color: rgba(0, 180, 42, 0.15) !important;
}

.navbar-container :deep(.el-dropdown-menu__item:focus) {
  color: #ffffff !important;
  background-color: rgba(0, 180, 42, 0.15) !important;
}

/* 直接覆盖可能的链接样式 */
.navbar-container :deep(.el-dropdown-menu a) {
  color: #ffffff !important;
  font-weight: 700 !important;
  font-size: 14px !important;
  text-decoration: none !important;
}

.navbar-container :deep(.el-dropdown-menu a:hover) {
  color: #ffffff !important;
  text-decoration: none !important;
}

.navbar-container :deep(.el-dropdown-menu a:visited) {
     color: #ffffff !important;
     text-decoration: none !important;
   }

/* 响应式设计 */
@media (max-width: 1200px) {
  .container {
    max-width: 1000px;
  }
  
  .nav-menu {
    gap: 20px;
  }
}

@media (max-width: 1024px) {
  .container {
    max-width: 900px;
    padding: 0 16px;
  }
  
  .search-box-large {
    max-width: 500px;
  }
  
  .logo {
    font-size: 24px;
    margin-right: 30px;
  }
  
  .icon-large {
    font-size: 28px;
  }
}

@media (max-width: 768px) {
  .container {
    padding: 0 12px;
    height: 60px;
  }
  
  .nav-menu {
    display: none;
  }
  
  .search-box-large {
    max-width: 100%;
    padding: 0 12px;
  }
  
  .logo-text {
    display: none;
  }
  
  .logo {
    margin-right: 20px;
  }
  
  .search-container {
    padding: 12px 0;
  }
}

@media (max-width: 480px) {
  .username {
    display: none;
  }
  
  .user-info {
    padding: 6px;
  }
  
  .container {
    padding: 0 10px;
  }
  
  :deep(.el-button) {
    font-size: 13px;
    padding: 6px 12px;
  }
}
</style>